package com.sharein.db;

import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

import java.sql.Connection;
import java.sql.DriverManager;

public class DataSource {
	private String driver, bridge;
	private String name, host;
	private String user, password;
	
	public DataSource() { // extraction of database.xml
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	        //Document doc = docBuilder.parse (new File("xml/database.xml"));
	        Document doc = docBuilder.parse (new File("../../../../workspace/Sharein/WebContent/xml/database.xml"));
            doc.getDocumentElement ().normalize (); // normalize text representation
            Node dbdriver = doc.getElementsByTagName("driver").item(0);
            driver = dbdriver.getChildNodes().item(0).getNodeValue().trim();
            Node dbbridge = doc.getElementsByTagName("bridge").item(0);
            bridge = dbbridge.getChildNodes().item(0).getNodeValue().trim();
            Node dbname = doc.getElementsByTagName("name").item(0);
            name = dbname.getChildNodes().item(0).getNodeValue().trim();
            Node dbhost = doc.getElementsByTagName("host").item(0);
            host = dbhost.getChildNodes().item(0).getNodeValue().trim();
            Node dbuser = doc.getElementsByTagName("user").item(0);
            user = dbuser.getChildNodes().item(0).getNodeValue().trim();
            Node dbpassword = doc.getElementsByTagName("password").item(0);
            password = dbpassword.getChildNodes().item(0).getNodeValue().trim();
		}catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());
	    }catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();
	    }catch (Throwable t) {
	        t.printStackTrace ();
	    }
	}
	
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public void setBridge(String bridge) {
		this.bridge = bridge;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUser() {
		return user;
	}
	public String getPassword() {
		return password;
	}
	public String getDriver() {
		return driver;
	}
	
	public String getBridge() {
		return bridge;
	}

	public String getName() {
		return name;
	}
	
	public String getHost() {
		return host;
	}
	
	public String getURL() {
		return getBridge() + "//" + getHost() + "/" + getName();
	}
	
	public Connection getConnection() {
		try {
			Class.forName(getDriver());
			Connection con = DriverManager.getConnection(getURL(), getUser(), getPassword());
			System.out.println("Connection established...");
			return con;
		}
		catch(Exception e) {
			System.out.println("Connection error : " + e.getMessage());
			return null;
		}
	}
}
